var fn = require('function.js');
App({
  // getUserInfo: function (cb) {
  //     var that = this
  //     if (this.globalData.userInfo) {
  //         typeof cb == "function" && cb(this.globalData.userInfo)
  //     } else {
  //        
  //         wx.login({
  //             success: function () {o
  //                 wx.getUserInfo({
  //                     success: function (res) {
  //                         that.globalData.userInfo = res.userInfo
  //                         typeof cb == "function" && cb(that.globalData.userInfo)
  //                     }
  //                 })
  //             }
  //         })
  //     }
  // },

  globalData: {
    auth:false,
    wxUserInfo: [],
    wxNewUserInfo: [],
    userInfo: null,
    uid: '',
    userWxInfo: {
      user_id: '',
      unionid: '',
      openid: '',
    },
    payStatus :'',//支付状态
    version: '1.3',
    unionid: '',
    lat:'',
    lng: '',
    openid: '',
    userCode: '',
    encryptedData: '',
    userIv: '',
  },


  showMsg: function (msg) { 
   
    wx.showLoading({
      title: msg,
      mask: true
    });
    setTimeout(function(){
     wx.hideLoading();
    },1500);
  
  },


  checkSession: function () {
    var that = this;

    wx.checkSession({
      success: function () {
       
        that.getCache();
        console.log("无缓冲");
      },
      fail: function () {
      

        try {
          wx.clearStorageSync()
          console.log("清除");
        } catch (e) {
       
        }
        console.log("登录");

        that.getLogin();
      }
    })

  },


  getCache: function () {

    
    try {
      var value = wx.getStorageSync('wx_user_info')
      if (value) {
 
   
        this.globalData.wxUserInfo = value;

   
        try {
          var value = wx.getStorageSync('auth_info')
          if (value) {
            this.globalData.openid = value.openid;
            this.globalData.unionid = value.unionid;

            try {
              var value = wx.getStorageSync('user_info')
              if (value) {
           
                this.globalData.uid = value.uid;
                this.globalData.userInfo = value;
              } 
            } catch (e) {}
          } else { 
          
            this.getAuthData();
          }
        } catch (e) {
  
        }
      }
      console.log("登录获取");
      this.getLogin();
    } catch (e) {
    
    }

  },


 
  getLogin: function () {
    var that = this;
    wx.login({
      success: function (res) {

        that.globalData.userCode = res.code;

        that.getAuthData();

      },
      fail: function (res) {
    
      },
    })
  },


  getWxUserInfo: function () {
    var that = this;
    wx.getUserInfo({
      success: function (res) {
      

        var rawData = JSON.parse(res.rawData);
   

      
        wx.setStorage({
          key: "wx_user_info",
          data: rawData
        })
        that.globalData.wxUserInfo = rawData;
        that.globalData.encryptedData = res.encryptedData;
        that.globalData.userIv = res.iv;
        that.getAuthData();
      
      },
    })
  },


  getAuthData: function () {
    var that = this;

    var appid = 'wxb3b1d577ae9d2eb0'; 
    var secret = '02eb410ed7be66b71382ff6d42727bba'; 

    fn.apiData({
      url: 'user.php',
      data: {
        act:'wxLogin',
        code: that.globalData.userCode,
        nickname: that.globalData.wxUserInfo.nickName,
        logo: that.globalData.wxUserInfo.avatarUrl,
      },
      success: function (res) {
      
    
        wx.hideToast();



     
        wx.setStorage({
          key: "auth_info",
          data: that.globalData.wxUserInfo
        })
     


        that.globalData.uid = res.data.data.user_id; 

        wx.setStorageSync('user_id', that.globalData.uid);
        wx.setStorageSync("user_code", that.globalData.userCode);
        // wx.setStorageSync(key, data)
        // console.log(that.globalData.uid + "执行登录接口2222222" + wx.getStorageSync("user_code") );


        wx.setStorage({
          key: "wx_user_info",
          data: that.globalData.wxUserInfo
        })
     

        if (res.data.success) {
     
        }
      }
    });
  },


  getUserInfo: function (cb) {
    var that = this;
    fn.apiData({
      url: 'myInfo.php',
      data: {
        unionid: that.globalData.openid
      },
      success: function (res) {
     

        if (res.data.success) {

    
          wx.setStorage({
            key: "user_info",
            data: res.data.result
          })
       
          that.globalData.uid = res.data.result.uid;
          that.globalData.userInfo = res.data.result;

          typeof cb == "function" && cb(that.globalData.userInfo)
        } else {

       
          try {
            wx.removeStorageSync('user_info')
           
          } catch (e) {
          
          }
        }
     
        wx.hideToast();
      }
    });
  },



  fn: fn
})
